<!DOCTYPE html>
<html>

<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=0,minimum-scale=1.0,maximum-scale=1.0" />
  <meta name="author" content="火星科技 http://mars3d.cn ">
  <meta name="apple-touch-fullscreen" content="yes">
  <meta name="apple-mobile-web-app-capable" content="yes">
  <meta name="apple-mobile-web-app-status-bar-style" content="black">
  <meta name="format-detection" content="telephone=no">
  <meta name="x5-fullscreen" content="true">
  <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1" />
  <!-- 标题及搜索关键字 -->
  <meta name="keywords" content="火星科技,cesium,3D,GIS,marsgis,三维,地球,地图,开发,框架,系统,示例,资料,模型,离线,外包,合肥,安徽,中国" />
  <meta name="description"
        content="火星科技 合肥火星 合肥火星科技 合肥火星科技有限公司 leaflet leaflet框架 leaflet开发 cesium cesium开发 cesium框架 三维 地球 模型  gis marsgis 地图离线 地图开发 地图框架 地图外包 框架 开发 外包  地图离线 二维地图 三维地图 全景漫游 地理信息系统 云GIS 三维GIS GIS平台 WebGIS" />

  <link rel="shortcut icon" type="image/x-icon" href="http://mars3d.cn/favicon.ico">
  <title>POI查询 |Mars3D|三维地图| 火星科技|合肥火星科技|合肥火星科技有限公司</title>


  <!--第三方lib-->
  <script type="text/javascript" src="../lib/include-lib.js" libpath="../lib/"
    include="jquery,font-awesome,web-icons,bootstrap,bootstrap-table,bootstrap-checkbox,layer,toastr,haoutil,turf,mars3d">
  </script>

  <link rel="stylesheet" href="../lib/city-picker/city-picker.css">
  <script src="../lib/city-picker/city-picker.data.js"></script>
  <script src="../lib/city-picker/city-picker.js"></script>

  <link href="css/style.css" rel="stylesheet" />
  <style>
    .city-picker-span {
      display: block;
      margin: 0 auto;
    }

    .form-control {
      max-width: 80%;
    }
  </style>
</head>

<body class="dark">
  <!--加载前进行操作提示，优化用户体验-->
  <div id="mask" class="signmask" onclick="removeMask()"></div>

  <div id="mars3dContainer" class="mars3d-container"></div>


  <div class="infoview">

    <div>
      <div>
        <span>范围：</span>
        <div class="radio radio-info radio-inline">
          <input type="radio" id="queryContent1" name="queryContent" value="1" checked>
          <label for="queryContent1">指定城市</label>
        </div>
        <div class="radio radio-info radio-inline">
          <input type="radio" id="queryContent2" name="queryContent" value="2">
          <label for="queryContent2">当前视域附近</label>
        </div>
        <div class="radio radio-info radio-inline">
          <input type="radio" id="queryContent3" name="queryContent" value="3">
          <label for="queryContent3">指定范围</label>
        </div>
      </div>
      <div style="position: relative;margin-top: 10px" class="queryByCity">
        <input id="txtCity" readonly type="text" placeholder="请选择城市" style="width: 100%;">
      </div>
      <div class="queryByDraw" style="display: none;margin-top: 10px;text-align: center">
        <input type="button" class="btn btn-primary" value="框选范围" id="drawRectangle" />
        <input type="button" class="btn btn-primary" value="圆形范围" id="drawCircle" />
        <input type="button" class="btn btn-primary" value="多边形范围" id="drawPolygon" />
      </div>
      <div style="margin-top: 10px;"><span>名称：</span><input id="queryText" type="text" placeholder="请输入查询关键字" value="" class="form-control "></div>
    </div>

    <div style="margin-top: 10px;margin-bottom: 10px;text-align: center;">
      <input type="button" class="btn btn-primary" value="查询" id="query" />
      <input type="button" class="btn btn-primary" value="清除" id="removeAll" />
    </div>

    <div id="resultView" style="max-width:500px;display: none">
      <table id="table"></table>
      <div>
        <input type="button" class="btn btn-primary" value="加载更多" id="loadMore" />
        <div style="float: right;">
          大约&nbsp;&nbsp;<span style="color: red;" id="allcount">0</span>&nbsp;&nbsp;条数据，
          已加载&nbsp;&nbsp;<span style="color: red;" id="count">0</span>&nbsp;&nbsp;条数据，
        </div>
      </div>
    </div>

  </div>


  <script src="./js/common.js"></script>
  <script type="text/javascript">
    'use script' //开发环境建议开启严格模式

    var map
    var poiLayer
    var queryGaodePOI

    var drawGraphic //限定区域

    var $table
    var bootstrapList = [] //查询结果
    var lastQueryOptions //上一次请求参数，用于 下一页使用

    function initMap(options) {
      //合并属性参数，可覆盖config.json中的对应配置
      var mapOptions = mars3d.Util.merge(options, {
        scene: {
          center: {
            y: 31.797919,
            x: 117.281329,
            z: 36235.64,
            heading: 357.9,
            pitch: -81.4,
            roll: 0,
          },
        },
      })

      //创建三维地球场景
      map = new mars3d.Map('mars3dContainer', mapOptions)

      //创建矢量数据图层
      poiLayer = new mars3d.layer.GraphicLayer()
      map.addLayer(poiLayer)

      poiLayer.bindPopup(
        function (event) {
          var item = event.graphic.attr

          var inHtml = '<div class="mars-popup-titile">' + item.name + '</div><div class="mars-popup-content" >'
          var type = $.trim(item.type)
          if (type != '') {
            inHtml += '<div><label>类别</label>' + type + '</div>'
          }
          var xzqh = $.trim(item.xzqh)
          if (xzqh != '') {
            inHtml += '<div><label>区域</label>' + xzqh + '</div>'
          }
          var tel = $.trim(item.tel)
          if (tel != '') {
            inHtml += '<div><label>电话</label>' + tel + '</div>'
          }
          var address = $.trim(item.address)
          if (address != '') {
            inHtml += '<div><label>地址</label>' + address + '</div>'
          }
          inHtml += '</div>'
          return inHtml
        },
        {
          anchor: [0, -10],
        }
      )

      queryGaodePOI = new mars3d.query.GaodePOI({
        // key: ['ae29a37307840c7ae4a785ac905927e0'],
      })

      bindViewEvent()
    }

    function bindViewEvent() {
      $('#txtCity').citypicker({
        simple: true,
        level: 'city',
        province: '安徽省',
        city: '合肥市',
        //district: '蜀山区',
      })

      $table = $('#table')
      $table.bootstrapTable({
        height: 300,
        singleSelect: true, //单选
        pagination: false,
        iconsPrefix: 'fa',
        columns: [
          {
            title: '序号',
            sortable: true,
            editable: false,
            align: 'left',
            formatter: function (value, row, index) {
              return index + 1
            },
          },
          {
            field: 'name',
            title: '企业名称',
          },
          {
            field: 'address',
            title: '地址',
          },
          {
            field: 'tel',
            title: '联系电话',
            sortable: true,
            editable: false,
            align: 'left',
            formatter: function (value, row) {
              if (value) {
                return value
              } else {
                return ''
              }
            },
          },
        ],
        onClickRow: function (item, $element, field) {
          var graphic = item._graphic
          if (graphic == null) {
            toastr.warning(item.name + ' 无经纬度坐标信息！')
            return
          }
          var position = graphic.positionShow
          poiLayer.closePopup()

          map.flyToPoint(position, {
            radius: 2000, //距离目标点的距离
            duration: 3,
            complete: function (e) {
              //飞行完成回调方法
              poiLayer.openPopup(graphic)
            },
          })
        },
      })

      var selectType
      $('input:radio[name="queryContent"]').change(function () {
        selectType = $(this).val()

        switch (selectType) {
          default:
            //按城市
            $('.queryByCity').show()
            $('.queryByDraw').hide()
            break
          case '2': //当前视角范围
            $('.queryByCity').hide()
            $('.queryByDraw').hide()
            break
          case '3': //按范围
            $('.queryByCity').hide()
            $('.queryByDraw').show()
            break
        }
      })

      $('#query').click(function () {
        clearAll(selectType == '3')
        bootstrapList = []
        switch (selectType) {
          default:
            //按城市
            var dnnm = $('#txtCity').data('citypicker').getCode()
            // var dmmc = $("#txtCity").data('citypicker').getVal();

            loadData({
              page: 1,
              city: dnnm,
              citylimit: true,
            })
            break
          case '2': //当前视角范围
            var extent = map.getExtent()
            loadData({
              page: 1,
              points: [
                [extent.xmin, extent.ymin],
                [extent.xmin, extent.ymax],
                [extent.xmax, extent.ymax],
                [extent.xmax, extent.ymin],
              ],
            })
            break
          case '3': //按范围
            if (!drawGraphic) {
              toastr.warning('请绘制限定范围！')
              return
            }
            loadData({
              page: 1,
              graphic: drawGraphic,
            })
            break
        }
      })

      //框选查询 矩形
      $('#drawRectangle').click(function () {
        clearAll()
        map.graphicLayer.startDraw({
          type: 'rectangle',
          style: {
            color: '#00FF00',
            opacity: 0.3,
            outline: true,
            outlineColor: '#ffffff',
            clampToGround: true,
          },
          success: function (graphic) {
            drawGraphic = graphic
          },
        })
      })

      //框选查询   多边
      $('#drawPolygon').click(function () {
        clearAll()
        map.graphicLayer.startDraw({
          type: 'polygon',
          style: {
            color: '#00FF00',
            opacity: 0.3,
            outline: true,
            outlineColor: '#ffffff',
            clampToGround: true,
          },
          success: function (graphic) {
            drawGraphic = graphic
          },
        })
      })

      //框选查询   圆
      $('#drawCircle').click(function () {
        clearAll()
        map.graphicLayer.startDraw({
          type: 'circle',
          style: {
            color: '#00FF00',
            opacity: 0.3,
            outline: true,
            outlineColor: '#ffffff',
            clampToGround: true,
          },
          success: function (graphic) {
            drawGraphic = graphic
          },
        })
      })

      $('#removeAll').click(function () {
        clearAll()
      })

      //加载更多- 下一页
      $('#loadMore').click(function () {
        if (!lastQueryOptions) {
          return
        }

        lastQueryOptions.page++
        loadData(lastQueryOptions)
      })
    }

    function loadData(queryOptions) {
      var queryVal = $.trim($('#queryText').val())
      if (queryVal.length == 0) {
        haoutil.msg('请输入 名称 关键字筛选数据！')
        return
      }
      haoutil.loading.show()

      lastQueryOptions = {
        ...queryOptions,
        count: 25, //count 每页数量
        text: queryVal,
        success: function (res) {
          $('#resultView').show()
          var data = res.list
          bootstrapList = bootstrapList.concat(data)
          $table.bootstrapTable('append', data)
          $('#count').html(bootstrapList.length)
          $('#allcount').html(res.allcount)
          if (bootstrapList.length >= res.allcount || res.allcount < 25 || data.length == 0) {
            $('#loadMore').hide()
          } else {
            $('#loadMore').show()
          }
          addGraphics(data)
          haoutil.loading.close()
        },
        error: function (msg, error) {
          window.toastr.error(msg)
          haoutil.loading.close()
        },
      }
      queryGaodePOI.query(lastQueryOptions)
    }

    function clearAll(noClearDraw) {
      lastQueryOptions = null

      bootstrapList = []
      $table.bootstrapTable('load', bootstrapList)

      $('#resultView').hide()
      $('#loadMore').hide()

      poiLayer.clear()

      drawGraphic = null
      map.graphicLayer.clear()
    }

    function addGraphics(arr) {
      console.log('查询数据结果', arr)

      for (var i = 0; i < arr.length; i++) {
        var item = arr[i]

        var graphic = new mars3d.graphic.BillboardEntity({
          position: Cesium.Cartesian3.fromDegrees(item.x, item.y),
          style: {
            image: 'img/marker/mark3.png',
            scale: 1,
            scaleByDistance: true,
            scaleByDistance_far: 20000,
            scaleByDistance_farValue: 0.5,
            scaleByDistance_near: 1000,
            scaleByDistance_nearValue: 1,
            clampToGround: true, //贴地
            label: {
              text: item.name,
              font: '20px 楷体',
              style: Cesium.LabelStyle.FILL_AND_OUTLINE,
              fillColor: Cesium.Color.AZURE,
              outlineColor: Cesium.Color.BLACK,
              outlineWidth: 2,
              horizontalOrigin: Cesium.HorizontalOrigin.CENTER,
              verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
              pixelOffset: new Cesium.Cartesian2(0, -30), //偏移量
              distanceDisplayCondition: new Cesium.DistanceDisplayCondition(0.0, 200000),
              clampToGround: true, //贴地
            },
          },
          attr: item,
        })
        poiLayer.addGraphic(graphic)

        item._graphic = graphic
      }
    }
  </script>
</body>

</html>
